package com.babasport.service.product.impl;

import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.babasport.service.base.DAOSupport;
import com.babasport.service.product.ProductInfoService;

@Service
// 是一个service
@Transactional
// 开启事务
public class ProductInfoServiceImpl extends DAOSupport implements
		ProductInfoService {

	@Override
	public <T> void delete(Class<T> entityClass, Object entityID) {
		this.delete(entityClass, new Object[] { entityID });
	}

	@Override
	public <T> void delete(Class<T> entityClass, Object[] entityIDs) {
		if (entityIDs != null && entityIDs.length != 0) {
			StringBuilder jsql = new StringBuilder();
			for (int i = 0; i < entityIDs.length; i++) {
				jsql.append("?").append(i + 2).append(",");
			}
			jsql.deleteCharAt(jsql.length() - 1);
			Query query = em.createQuery(
					"update ProductInfo t set t.visible=?1 where t.id in ("
							+ jsql.toString() + ")").setParameter(1, false);
			for (int i = 0; i < entityIDs.length; i++) {
				query.setParameter(i + 2, entityIDs[i]);
			}
			query.executeUpdate();
		}
	}

}
